﻿using EnterpriseWebsiteApi.ActionResults;
using EnterpriseWebsiteApi.Dto.User;
using EnterpriseWebsiteApi.Model;
using EnterpriseWebsiteBLL;
using EnterpriseWebsiteUtil.Helper;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using EnterpriseWebsiteApi.ActionFilters;
using EnterpriseWebsiteEntity;

namespace EnterpriseWebsiteApi.Controllers.Admin
{
    [Route("api/admin/[controller]/[action]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        private readonly JWTHelper _jwtHelper;
        private readonly Mapper _mapper;
        private readonly UserService _userService;

        public UserController(JWTHelper jwtHelper, Mapper mapper, UserService userService)
        {
            _jwtHelper = jwtHelper;
            _mapper = mapper;
            _userService = userService;
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> Login(LoginDTO login)
        {
            try
            {
                if (string.IsNullOrEmpty(login.UserName) || string.IsNullOrEmpty(login.PassWord))
                {
                    return new Error("账号或密码不能为空");
                }
                var entity = await _userService.LoginAsync(login.UserName, login.PassWord);
                if (entity != null)
                {
                    User user = _mapper.Map<User>(entity);
                    var jwt = _jwtHelper.GenerateJWT(user);
                    return new Success(jwt);
                }
                else
                {
                    return new Error("账户或者密码错误");
                }
            }
            catch (Exception ex)
            {
                return new Error(ex);
            }
        }

        /// <summary>
        /// 身份验证
        /// </summary>
        /// <returns></returns>
        [TypeFilter(typeof(JwtAuthorizeFilter))]
        [HttpGet]
        public ActionResult Authentication()
        {
            return new Success();
        }

        /// <summary>
        /// 登出
        /// </summary>
        /// <returns></returns>
        [TypeFilter(typeof(JwtAuthorizeFilter))]
        [HttpGet]
        public  async Task<ActionResult> Logout()
        {
            try
            {
                var @operator = HttpContext.Items["operator"] as UserEntity;
                if (@operator == null)
                {
                    return new Unauthorized();
                }
                await _userService.LogoutAsync(@operator.Id);
                return new Success();
            }
            catch (Exception ex)
            {
                return new Error(ex);
            }
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [TypeFilter(typeof(JwtAuthorizeFilter))]
        [HttpPost]
        public async Task<ActionResult> ChangePassword(ChangePasswordDTO dto)
        {
            try
            {
                var @operator = HttpContext.Items["operator"] as UserEntity;
                if (@operator == null)
                {
                    return new Unauthorized();
                }
                await _userService.ChangeUserName(@operator.Id, dto.UserName);
                await _userService.ChangePasswordAsync(@operator.Id, dto.PassWordOld,dto.PassWordNew,dto.PassWordConfirm);
                await _userService.LogoutAsync(@operator.Id);
                return new Success();
            }
            catch (Exception ex)
            {
                return new Error(ex);
            }
        }

        /// <summary>
        /// 获取用户名
        /// </summary>
        /// <returns></returns>
        [TypeFilter(typeof(JwtAuthorizeFilter))]
        [HttpGet]
        public ActionResult GetUserName()
        {
            try
            {
                var @operator = HttpContext.Items["operator"] as UserEntity;
                if (@operator == null)
                {
                    return new Unauthorized();
                }
                return new Success(@operator.UserName);
            }
            catch (Exception ex)
            {
                return new Error(ex);
            }
        }
    }
}
